import rendering,pygame
import entityManager
from vector import vector
def isInside(start,end):
    mPos = pygame.mouse.get_pos()
    if mPos[0] > start.get()[0] and mPos[1] > start.get()[1] and mPos[0] < end.get()[0] and mPos[1] < end.get()[1]:
        return True
    else:
        return False
class window:
    def __init__(self,surface,pos,callOnKill = None,size = (200,200)):
        entityManager.entityList.append(self)
        self.body = rendering.i_object("./GUI/Gui_base.png",surface)
        self.size = size
        self.body.scale(size[0],size[1])
        self.surface = surface
        self.killFunc = callOnKill
        self.buttonList = []
        self.pos = pos
        self.buttonList.append(button(surface,vector(self.pos.get()[0]+self.size[0]-15,self.pos.get()[1]),"x",self.kill))
        self.newPos = 25
        self.oldMousePos = vector(0,0)
    def kill(self):
        if self.killFunc != None:
            self.killFunc()
        entityManager.buffer.remove(self)
        del self
    def moveWindow(self):
        x = pygame.mouse.get_pos()[0] - self.oldMousePos.get()[0]
        y = pygame.mouse.get_pos()[1] - self.oldMousePos.get()[1]
        self.pos = vector(self.pos.get()[0]+x,self.pos.get()[1]+y)
        for button in self.buttonList:
            button.pos = vector(button.pos.get()[0]+x,button.pos.get()[1]+y)
    def addButton(self,txt,function):
        self.buttonList.append(button(self.surface,vector(self.pos.get()[0]+10,self.pos.get()[1]+self.newPos),txt,function))
        self.newPos = self.newPos + 25
    def think(self):
        if isInside(self.pos,vector(self.pos.get()[0]+self.size[0],self.pos.get()[1]+25)) and pygame.mouse.get_pressed()[0] == 1:
            self.moveWindow()
        self.body.draw(self.pos)
        for button in self.buttonList:
            button.think()
        self.oldMousePos = vector(pygame.mouse.get_pos()[0],pygame.mouse.get_pos()[1])
class txt:
    def __init__(self,surface,txt,pos,space):
        self.body = text
class button:
    def __init__(self,screen,pos,txt,function):
        self.pos = pos
        self.body = rendering.text(screen,txt,(0,0,255))
        self.function = function
    def draw(self):
        self.body.draw(vector(self.pos.get()[0],self.pos.get()[1]))
    def think(self):
        self.draw()
        self.body.color = (0,0,255)
        if isInside(self.pos,vector(self.pos.get()[0]+self.body.getWidth(),self.pos.get()[1]+25)):
            self.body.color = (255,255,255)
            if pygame.mouse.get_pressed()[0] == 1:
                self.function()
